//
//  LinkView.swift
//  Chatup
//
//  Created by iron man on 2024/1/15.
//

import SwiftUI

struct LinkView: View {
    @Environment(\.presentationMode) var presentationMode
    @State private var imaginationText: String = ""
    @State private var link: String = ""
    @State var showVIP = false
    @State private var showAlert: Bool = false
    @State var isVip = UserDefaults.standard.bool(forKey: "kWhetherVip")
    var body: some View {
        VStack {
            Spacer().frame(height: 16)
            HStack {
                Button {
                    presentationMode.wrappedValue.dismiss()
                } label: {
                    Image("nav_close")
                        .resizable()
                        .frame(width: 12, height: 12)
                }
                Spacer()
                if !isVip{
                    Button {
                        showVIP.toggle()
                    } label: {
                        Image("icon_setting_vip_right")
                            .resizable()
                            .frame(width: 77, height: 24)
                    }
                }

            }.padding(.horizontal, 22)
                
            Spacer().frame(height: 16)
                
            Text("🔗"+String.i18nText("feature_analyze_link"))
                .font(.system(size: 28).weight(.bold))
                .foregroundColor(.white)
            Text(String.i18nText("feature_analyze_link_hint1"))
                .font(.system(size: 16))
                .foregroundColor(Color(red: 0.51, green: 0.51, blue: 0.51))
                .frame(width: 316, height: 71, alignment: .topLeading)
                
            Spacer().frame(height: 26)
            HStack{
                Spacer().frame(width: 22)
                Text("🔗 ")
                    .font(.system(size: 18))
                  .foregroundColor(Color(red: 0.61, green: 0.61, blue: 0.61))
                  .frame(width: 22, height: 28, alignment: .topLeading).padding(.vertical,18)
                  
                  TextField(String.i18nText("feature_analyze_link_hint2"), text: $link)
                    .font(.system(size: 18))
                    .padding(.vertical, 0)

            }.background(Color(red: 0.13, green: 0.13, blue: 0.14))
                .cornerRadius(10).padding(.horizontal, 22)
                
            Spacer().frame(height: 20)
            // 文本输入
                
            TextView(hint: String.i18nText("feature_recognize_images_hint2"), text: $imaginationText).padding(.horizontal,23)
                
            Spacer()

            // 'Draw' 按钮
            Button(action: {
                isVip = UserDefaults.standard.bool(forKey: "kWhetherVip")
                if !isVip {
                    showVIP.toggle()
                    return
                }
                LinkMessageBuilder().buildMessage(resouse: link + "#" + imaginationText, message: {msg in
                    guard msg != nil else {
                        presentationMode.wrappedValue.dismiss()
                        return
                    }
                    if msg == "-1"{
                        showAlert = true
                        return
                    }
                    let msgModel = MsgModel(type: .user, messageType: .text, message: link + imaginationText, roleId: 0, isLike: false, isUnlike: false,context: link + imaginationText)
                
                    
                    DispatchQueue.main.async {
                        NotificationCenter.default.post(name: Notification.Name("kAppendMessage"), object: ["feature":FeaturesType.link,"msgs":[msgModel]])
                        presentationMode.wrappedValue.dismiss()
                    }
                })
                presentationMode.wrappedValue.dismiss()
            }) {
                Text(String.i18nText("feature_analyze"))
                    .font(.system(size: 18).weight(.bold))
                    .foregroundColor(Color(red: 1, green: 1, blue: 1))
                    .foregroundColor(.clear)
                    .frame(minWidth: 0, maxWidth: .infinity)
                    .frame(height: 57)
                    .background(
                        imaginationText.isEmpty ? Color(red: 0.54, green: 0.54, blue: 0.55):
                        Color(red: 0.15, green: 0.75, blue: 0.46)
                    )
                    .cornerRadius(12)
            }
            .padding()

            Spacer().frame(height: 32)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.black.opacity(0.8))
        .edgesIgnoringSafeArea(.all)
        .fullScreenCover(isPresented: $showVIP, content: {
            VipView(viewModel: .init(from: "feature_analyze_link"))
        })
        .onReceive(NotificationCenter.default.publisher(for: Notification.Name("refreshVip"))) { _ in
            isVip = UserDefaults.standard.bool(forKey: "kWhetherVip")
        }
        .onTapGesture {
            NotificationCenter.default.post(name: Notification.Name("kChangeFocused"), object: nil)
        }.alert(isPresented: $showAlert) {
            // 创建Alert实例
            Alert(
                title: Text(""),
                message: Text(String.i18nText("chat_no_credit_gpt4.0")),
                dismissButton: .default(Text(String.i18nText("continue"))) {
                    // 点击好的按钮后的操作
                    
                }
            )
        }
    }
}
